<template>
  <div class="upload-view">
    <div class="upload-container">
      <div class="upload-area">
        <el-upload
          class="upload-drop"
          drag
          action="/api/upload"
          :on-success="handleUploadSuccess"
          :on-error="handleUploadError"
          :before-upload="beforeUpload"
          accept=".dcm,.nii,.nii.gz"
          :limit="1"
        >
          <el-icon class="upload-icon"><Upload /></el-icon>
          <div class="upload-text">
            <h3>拖拽文件到此处或点击上传</h3>
            <p>支持上传 DICOM、NIfTI 格式文件，单个文件不超过500MB</p>
          </div>
        </el-upload>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ElMessage } from 'element-plus'
import { Upload } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'

const router = useRouter()

const beforeUpload = (file: File) => {
  const isValidFormat = /\.(dcm|nii|nii\.gz)$/i.test(file.name)
  const isLt500M = file.size / 1024 / 1024 < 500

  if (!isValidFormat) {
    ElMessage.error('只能上传 DICOM 或 NIfTI 格式的文件!')
    return false
  }
  if (!isLt500M) {
    ElMessage.error('文件大小不能超过 500MB!')
    return false
  }
  return true
}

const handleUploadSuccess = (response: any) => {
  ElMessage.success('文件上传成功')
  router.push({
    name: 'viewer',
    state: { volumeData: response.data }
  })
}

const handleUploadError = () => {
  ElMessage.error('文件上传失败')
}
</script>

<style scoped>
.upload-view {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f7fa;
}

.upload-container {
  width: 100%;
  max-width: 800px;
  padding: 40px;
}

.upload-area {
  background: #fff;
  border-radius: 8px;
  padding: 40px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.upload-drop {
  width: 100%;
}

:deep(.el-upload-dragger) {
  width: 100%;
  height: 300px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.upload-icon {
  font-size: 48px;
  color: #409eff;
  margin-bottom: 20px;
}

.upload-text h3 {
  margin: 0;
  font-size: 18px;
  color: #303133;
}

.upload-text p {
  margin: 10px 0 0;
  font-size: 14px;
  color: #909399;
}
</style> 